/**
 * Created by Administrator on 2017/3/1 0001.
 */
var textName='';
var resource;
var mode;
var pixiStageW=740;
var pixiStageH=1136;
var pixiStage=new PIXI.Stage(0xFF0000);
var pixiUI;
var img_back;
var mapObj;
var PixiMain={
    create:function () {
        // var ratio=window.devicePixelRatio;

        var pixi_renderer=PIXI.autoDetectRenderer(pixiStageW,pixiStageH,{antialias:true,transparent:true,resolution:1});

        var div_pixi=document.getElementById('div_2d');
        div_pixi.appendChild(pixi_renderer.view);

        var preload=Preload.create();
        preload.init(pixiStage,pixiStageW,pixiStageH);
        var con_whole=new PIXI.Container();
        pixiStage.addChild(con_whole);

        document.addEventListener('Main_LoadComplete',gameStart);
        function gameStart(e) {
            resource=e.detail.resources;
             var game=Game.create();
             pixiStage.addChildAt(game.ui,0);
            pixiUI=game.ui;
            threeInit();
            // createMap()
            // img_back=Easy.CreateSprite('assets/images/img_back.png',0,1136-50,0,0,1);
            // img_back.interactive=true;
            // img_back.on('pointerdown',function () {
            //     hideMap();
            // })


            // showMap()
            document.getElementById('img_back').addEventListener('touchstart',function (e) {
                hideMap();
            });
            document.getElementById('img_back').addEventListener('mousedown',function (e) {
                hideMap();
            });
        }

        resizeHandlerPIXI();
        window.onresize = resizeHandlerPIXI;
        function resizeHandlerPIXI() {
            pixi_renderer.resize(window.innerWidth, window.innerHeight);
            mode='noBorder';
            switch (mode) {
                case 'exactFit':
                    pixiStage.scale.x = window.innerWidth / pixiStageW;
                    pixiStage.scale.y = window.innerHeight / pixiStageH;
                    break;
                case 'noBorder':
                    pixiStage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerWidth / pixiStageW) : (window.innerHeight / pixiStageH);
                    pixiStage.scale.y = pixiStage.scale.x;
                    break;
                case 'noScale':
                    pixiStage.scale.x = 1;
                    pixiStage.scale.y = 1;
                    break;
                case 'showAll':
                    pixiStage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerHeight / pixiStageH) : (window.innerWidth / pixiStageW);
                    pixiStage.scale.y = pixiStage.scale.x;
                    break;
            }
            pixiStage.x = (window.innerWidth - pixiStageW * pixiStage.scale.x) * .5;
            pixiStage.y = (window.innerHeight - pixiStageH * pixiStage.scale.y) * .5;

            document.getElementById('div_map').style.width=-pixiStage.x+(740* pixiStage.scale.x)+'px';
            document.getElementById('div_map').style.height=-pixiStage.y+(1136*pixiStage.scale.y)+'px';
            document.getElementById('img_back').style.width= pixiStage.scale.x*740+'px';
            document.getElementById('img_back').style.height= pixiStage.scale.y*50+'px';
            document.getElementById('img_back').style.left= pixiStage.scale.x*0+pixiStage.x+'px';
            document.getElementById('img_back').style.top= pixiStage.scale.y*(1136-50)+pixiStage.y+'px';

        };

        // var globalID;
        // globalID=
        requestAnimationFrame(animate);
        // cancelAnimationFrame(globalID);
        function animate() {
            requestAnimationFrame(animate);
            pixi_renderer.render(pixiStage);
            // pd.emitter.update(0.01);
            // for(var i=0;i<game.arr_circle.length;i++)
            // {
            //     if(game.arr_circle[i].ui.scale.x>5)
            // }
        }

    }
}
var lat;
var lon;
var geolocation;
function showMap()
{
    if(!mapObj)
    {
        mapObj = new AMap.Map('div_map', {
            resizeEnable: true,
            zoom:16,
            center: [121.450982,31.253565]
        });

        var marker = new AMap.Marker({
            icon : 'http://vdata.amap.com/icons/b18/1/2.png',//24px*24px
            position :[121.450982,31.253565],
            offset : new AMap.Pixel(-12,-12),
            map : mapObj
        });
        document.getElementById('img_back').style.display='block';
        //构造路线导航类

        getLocation();
        // var driving = new AMap.Driving({
        //     map: mapObj,
        //     panel: "panel"
        // });
        // // 根据起终点经纬度规划驾车导航路线
        // driving.search(new AMap.LngLat(121.4233455806, 31.2733537527), new AMap.LngLat(121.4533455806,31.2533537527));

    }
    else
    {
        geolocation.getCurrentPosition();
    }
    document.getElementById('div_map').style.zIndex=22;
    document.getElementById('div_map').style.display='block';
    document.getElementById('img_back').style.zIndex= 23;
    document.getElementById('div_2d').style.zIndex=0;
}


function hideMap()
{
    document.getElementById('img_back').style.zIndex=0;
    document.getElementById('div_map').style.zIndex=1;
    document.getElementById('div_map').style.display='none';
    document.getElementById('div_2d').style.zIndex=22;
}
function getLocation()
{
    mapObj.plugin('AMap.Geolocation', function () {
            geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            timeout: 10000,          //超过10秒后停止定位，默认：无穷大
            maximumAge: 0,           //定位结果缓存0毫秒，默认：0
            convert: true,           //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
            showButton: true,        //显示定位按钮，默认：true
            buttonPosition: 'LB',    //定位按钮停靠位置，默认：'LB'，左下角
            buttonOffset: new AMap.Pixel(0, 0),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            showMarker: true,        //定位成功后在定位到的位置显示点标记，默认：true
            showCircle: true,        //定位成功后用圆圈表示定位精度范围，默认：true
            panToLocation: true,     //定位成功后将定位到的位置作为地图中心点，默认：true
            zoomToAccuracy:true      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
        });
        mapObj.addControl(geolocation);
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', function (e) {
            lat=e.position.getLat();
            lon=e.position.getLng();
            var driving = new AMap.Driving({
                map: mapObj,
                panel: "panel"
            });
            // 根据起终点经纬度规划驾车导航路线
            console.log(lat,lon);
            driving.search(new AMap.LngLat(lon, lat), new AMap.LngLat(121.450982,31.253565));
        });//返回定位信息
        AMap.event.addListener(geolocation, 'error', function (e) {
            console.log(e);
            console.log(e.info);
        });      //返回定位出错信息
    });
}
